package com.dongjiantong.farmrecords.model;

import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import lombok.Data;

import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 *  收集单
 */

@Data
@Table(name = "djt_collect_reports")
public class DjtCollectReports extends BaseEntity implements Serializable {
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    /**
     * 单据编号
     */
    private String number;

    /**
     * 来源id
     */
    private Long source_id;

    /**
     * 来源name
     */
    private String source_name;

    /**
     * 操作人
     */
    private String operator;

    /**
     * 处理产品类型（动物／产品）
     */
    private Integer breed_second_type;

    private Integer goods_type;

    private Date date;

    /**
     * 品种／规格
     */
    private Integer specification;

    /**
     * 处理数量
     */
    private Float quantity;

    /**
     * 死亡原因
     */
    private Integer death_reason;

    /**
     * 视频快照
     */
    private String video_snap;

    private Long collect_point_id;

    private String collect_point_name;

    /**
     * 耳标号
     */
    @JsonIgnore
    private String ear_number_json;
    @Transient
    private List<String> ear_number;

    /**
     * 是否被选择
     */
    private Integer is_selected;

    private Integer status;

    private Integer delete_flag;

    private String remark;

    private Date created_at;

    private Date updated_at;

    @Transient
    private List<String> picList;

    @Transient
    private List<String> videoList;

    public void setEar_number_json(String ear_number_json) {
        this.ear_number_json = ear_number_json;

        if (Strings.isNullOrEmpty(ear_number_json)) {
            this.ear_number = null;
        } else {
            try {
                this.ear_number = objectMapper.readValue(ear_number_json,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setEar_number(List<String> ear_number) {
        this.ear_number = ear_number;

        if (ear_number == null || ear_number.isEmpty()) {
            this.ear_number_json = "[]";
        } else {
            try {
                this.ear_number_json = objectMapper.writeValueAsString(ear_number);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

}